import 'package:shared_preferences/shared_preferences.dart';

class Cache {
  SharedPreferences? prefs;
  static Cache? _instance;

  // SharedPreferences prefs;
  // static Cache _instance;

  Cache._() {
    init();
  }
  Cache._pre(SharedPreferences prefs) {
    this.prefs = prefs;
  }
  static Future<Cache?> preInit() async {
    if (_instance == null) {
      var prefs = await SharedPreferences.getInstance();
      _instance = Cache._pre(prefs);
    }
    return _instance;
  }

  ///因为是懒汉模式的单例，所以单例instance设置成可空
  ///getInstance中因为会有null时创建单例，所以返回instance时将其转换成非空
  static Cache? getInstance() {
    if (_instance == null) {
      _instance = Cache._();
    }
    return _instance;
  }

  void init() async {
    if (prefs == null) {
      prefs = await SharedPreferences.getInstance();
    }
  }
}
